package cn.wen.yinxiu.works.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cn.wen.yinxiu.common.convention.result.Results;
import cn.wen.yinxiu.works.vo.AddCommentVO;
import cn.wen.yinxiu.works.vo.CommentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.wen.yinxiu.works.entity.CommentEntity;
import cn.wen.yinxiu.works.service.CommentService;
import cn.wen.yinxiu.common.toolkit.PageUtils;
import cn.wen.yinxiu.common.convention.result.ResultEntity;

/**
 * @ClassName: CommentController
 * @Author 小飞
 * @Email coderwen22@163.com
 * @Date 2023-05-22 09:39:56
 * @Description: 作品的评论数据的Controller层
 */
@RestController
@RequestMapping("/web")
public class CommentController {

    @Autowired
    private CommentService commentService;


    /**
     * 获取该作品的评论信息
     *
     * @param worksId
     * @return
     */
    @GetMapping("/works/comment/list")
    public ResultEntity getCommentList(@RequestParam("worksId") Long worksId,
                                       @RequestParam("page") Integer page,
                                       @RequestParam("userId") Long userId) {
        List<CommentVO> commentList = commentService.listCommentByWorksId(worksId, page, userId);
        return Results.success(commentList);
    }

    /**
     * 发布评论 / 回复评论
     *
     * @param addCommentVO
     * @return
     */
    @PostMapping("/api/works/comment/add")
    public ResultEntity addComment(@RequestBody AddCommentVO addCommentVO) {
        boolean res = commentService.insertComment(addCommentVO);
        if (res) return Results.success();
        return Results.failure();
    }

    /**
     * 删除评论
     *
     * @param commendId
     * @return
     */
    @PostMapping("/api/works/comment/delete")
    public ResultEntity deleteComment(@RequestParam("commentId") Long commendId,
                                      @RequestParam("worksId") Long worksId) {
        boolean res = commentService.deleteCommentById(commendId, worksId);
        if (res) return Results.success();
        return Results.failure();
    }

    /**
     * 评论点赞
     *
     * @param commendId
     * @return
     */
    @PostMapping("/api/works/comment/like")
    public ResultEntity commentLike(@RequestParam("commentId") Long commendId,
                                    @RequestParam("worksId") Long worksId,
                                    @RequestParam("userId") Long userId) {
        boolean res = commentService.commentLikeById(commendId, worksId, userId);
        if (res) return Results.success();
        return Results.failure();
    }

    /**
     * 评论取消点赞
     *
     * @param commendId
     * @return
     */
    @PostMapping("/api/works/comment/unlike")
    public ResultEntity commentUnlike(@RequestParam("commentId") Long commendId,
                                      @RequestParam("worksId") Long worksId,
                                      @RequestParam("userId") Long userId) {
        boolean res = commentService.commentUnlikeById(commendId, worksId, userId);
        if (res) return Results.success();
        return Results.failure();
    }


}
